/*
 * Copyright (C) 2004, Velocityme Partnership
 * ShowTreeAction.java
 *
 * Created on 12 September 2004, 12:12
 */

package com.velocityme.www.interfaces;

import com.velocityme.entity.PermissionBean;
import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.www.dto.AccessDTO;
import com.velocityme.www.utility.NodeUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author  Robert
 */
public class ShowTreeAction extends DispatchAction {
    
    /** Creates a new instance of ShowTreeAction */
    public ShowTreeAction() {
    }
    
    public ActionForward home(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        // remove some critical attributes
        session.removeAttribute("nodeId");
        session.removeAttribute("nodeValue");
        session.removeAttribute("tabValues");
        session.removeAttribute("menuButtonValues");
        session.removeAttribute("buttonValues");
        session.removeAttribute("selectedTabValue");
        session.setAttribute("ancestorValues", cs.getVisibleAncestorValues(key, null));
        session.setAttribute("menuDTO", NodeUtil.getInstance().getMenu(key, null, null, false, false, false, false));
                
        ShowNodeAction.setupChildrenDTOs(session, null);
        session.setAttribute("nodeId", new Integer(0));
        session.setAttribute("tabValues", new String[]{});
        return mapping.findForward("ACTION_SUCCESS");
    }
    
    public ActionForward node(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        HttpSession session = request.getSession();
        KeySession key = null;
        try {
            key = (KeySession)session.getAttribute("sessionKey");
        }
        catch(ClassCastException e) {
            // this means that we are not currently logged in so ensure that we do
            key = null;
        }
        if (key == null) {
            session.setAttribute("nodePK", nodePK);
            return mapping.findForward("ACTION_LOGIN");
        }
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodeValue nodeValue = cs.getNodeValue(key, nodePK);
        ShowNodeAction.setupTreeDisplay(session, nodeValue, nodeValue.getFinalType());
        if (nodeValue.getType().equals("Contactable")) {
            String contactableType = nodeValue.getFinalType();
            if (contactableType.equals("Department")) {
                return mapping.findForward("ACTION_SHOW_DEPARTMENT_SUCCESS");
            }
            if (contactableType.equals("Organisation")) {
                return mapping.findForward("ACTION_SHOW_ORGANISATION_SUCCESS");
            }
            if (contactableType.equals("Person")) {
                return mapping.findForward("ACTION_SHOW_PERSON_SUCCESS");
            }
        }
        if (nodeValue.getType().equals("Group")) {
            return mapping.findForward("ACTION_SHOW_GROUP_SUCCESS");
        }
        if (nodeValue.getType().equals("Note")) {
            return mapping.findForward("ACTION_SHOW_NOTE_SUCCESS");
        }
        if (nodeValue.getType().equals("Role")) {
            return mapping.findForward("ACTION_SHOW_ROLE_SUCCESS");
        }
        if (nodeValue.getType().equals("Sprint")) {
            return mapping.findForward("ACTION_SHOW_SPRINT_SUCCESS");
        }
        if (nodeValue.getType().equals("State")) {
            return mapping.findForward("ACTION_SHOW_STATE_SUCCESS");
        }
        if (nodeValue.getType().equals("Status")) {
            return mapping.findForward("ACTION_SHOW_STATUS_SUCCESS");
        }
        if (nodeValue.getType().equals("Task")) {
            TaskPK taskPK = new TaskPK(nodePK.getNodeId());
            String taskType = nodeValue.getFinalType();
            if (taskType.equals("Action")) {
                return mapping.findForward("ACTION_SHOW_ACTION_SUCCESS");
            }
            if (taskType.equals("Anchor")) {
                return mapping.findForward("ACTION_SHOW_ANCHOR_SUCCESS");
            }
            if (taskType.equals("Issue")) {
                return mapping.findForward("ACTION_SHOW_ISSUE_SUCCESS");
            }
            if (taskType.equals("Project")) {
                return mapping.findForward("ACTION_SHOW_PROJECT_SUCCESS");
            }
        }
        if (nodeValue.getType().equals("TaskStateMachine")) {
            return mapping.findForward("ACTION_SHOW_TASKSTATEMACHINE_SUCCESS");
        }
        if (nodeValue.getType().equals("User")) {
            return mapping.findForward("ACTION_SHOW_USER_SUCCESS");
        }
        if (nodeValue.getType().equals("Node")) {
            String nodeType = nodeValue.getFinalType();
            if (nodeType.equals("SystemNode")) {
                session.setAttribute("nodeValue", nodeValue);
                return mapping.findForward("ACTION_SHOW_SYSTEMNODE_SUCCESS");
            }
            if (nodeType.equals("ContactDetailTypes")) {
                request.setAttribute("selectedTab", "ContactDetailTypes");
                return mapping.findForward("ACTION_SHOW_CONTACTDETAILTYPES_SUCCESS");
            }
            if (nodeType.equals("SystemProperties")) {
                request.setAttribute("selectedTab", "SystemProperties");
                return mapping.findForward("ACTION_SHOW_SYSTEMPROPERTY_SUCCESS");
            }
            if (nodeType.equals("LicenceManagement")) {
                request.setAttribute("selectedTab", "LicenceManagement");
                return mapping.findForward("ACTION_SHOW_LICENCEMANAGEMENT_SUCCESS");
            }
            if (nodeType.equals("LoginHistory")) {
                request.setAttribute("selectedTab", "LoginHistory");
                return mapping.findForward("ACTION_SHOW_LOGINHISTORY_SUCCESS");
            }
            if (nodeType.equals("Directory")) {
                request.setAttribute("selectedTab", "Directory");
                return mapping.findForward("ACTION_SHOW_DIRECTORY_SUCCESS");
            }
        }
        request.setAttribute("selectedTab", "Node");
        return mapping.findForward("ACTION_SHOW_NODE_SUCCESS");   
    }
    
}
